/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/11/2
 * Time: 12:04
 * Description:
 */
public class MyCircularQueue {
    private int[] elem;
    private int front;
    private int rear;//队尾的
    public MyCircularQueue(int k) {
        this.elem = new int[k];
    }
    //入队
    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        this.elem[this.rear] = value;
        this.rear =  (this.rear+1)%this.elem.length;
        return true;
    }
    //出队
    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        this.front = (this.front+1)%this.elem.length;
        return true;
    }
    //得到队头元素
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return this.elem[this.front];
    }
    //得到队尾元素
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        int index = -1;
        if(this.rear == 0) {
            index = elem.length-1;
        }else {
            index = this.rear-1;
        }
        return this.elem[index];
    }

    public boolean isEmpty() {
        return this.front == this.rear;
    }

    public boolean isFull() {
        return (this.rear+1)%this.elem.length == this.front;
    }
}
